package com.netx.cubigraf.apps.utilities;
import com.netx.generics.io.*;

import java.io.IOException;

public class PRJ10_DbRecoverer {

	public static void run() throws Throwable {
		PRJ10_DbRecoverer recoverer = new PRJ10_DbRecoverer();
		//FileSystem thumbs = new FileSystem("fs://localhost/[C]/Shared/05.Server/Tomcat/webapps/cubigraf2/Thumbnails");
		//recoverer.recoverReferences(thumbs);
		FileSystem machine = new FileSystem(new Location("//faria/Ficheiros"));
		recoverer.getDesignerNames(machine, "faria");
	}
	
	private final FileSystem _dest;
	
	public PRJ10_DbRecoverer() throws IOException {
		_dest = new FileSystem(new Location("C:/Shared/05.Server/01.Applications/Cubigraf/06.Temp"));
	}

	public void recoverReferences(FileSystem thumbs) throws IOException {
		File csv = _dest.createFile("references.csv");
		BufferedWriter writer = new BufferedWriter(csv.getOutputStream(false));
		writer.write("Ano;Mes;Referencia;Ref\n");
		String[] refs = thumbs.listFiles();
		for(int i=0; i<refs.length; i++) {
			String ref = refs[i].substring(0, 10);
			if(_after(ref, "05-11-3053")) {
				String[] array = ref.split("-");
				writer.write(array[0]+";"+array[1]+";"+array[2]+";"+ref+"\n");
			}
		}
		writer.close();
	}
	
	public void getDesignerNames(FileSystem machine, String designer) throws IOException {
		File csv = _dest.getFile("desenhadores.csv");
		BufferedWriter writer = new BufferedWriter(csv.getOutputStream(true));
		String[] refs = machine.listDirectories();
		for(int i=0; i<refs.length; i++) {
			if(!refs[i].equals("OLD")) {
				writer.write(refs[i]+";"+designer+"\n");
			}
		}
		refs = machine.getDirectory("OLD").listDirectories();
		for(int i=0; i<refs.length; i++) {
			writer.write(refs[i]+";"+designer+"\n");
		}
		writer.close();
	}
	
	private boolean _after(String ref1, String ref2) {
		return ref1.compareTo(ref2) > 0;
	}
}
